package com.helper.dream.a361phonehelper.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.bean.VideoBean;
import com.helper.dream.a361phonehelper.widget.CircleImage;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2016/12/21.
 */

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.MyHolder> {
    Context context;
    LayoutInflater layoutInflater;
    List<VideoBean.DataBean.ResBean> list;
    MediaPlayer player;
    int currentPosition = -1;

    public VideoAdapter(Context context, List<VideoBean.DataBean.ResBean> list) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.list = list;
        this.player = new MediaPlayer();
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.video_item_layout, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, int position) {
        VideoBean.DataBean.ResBean res = list.get(position);
        holder.tv_name.setText(res.getF());
        holder.tv_title.setText(res.getT());
        holder.frameLayout_top.setTag(position);
        Picasso.with(context).load(res.getI()).into(holder.iv_abbreviations);
        Picasso.with(context)
                .load(res.getFromicon())
                .transform(new CircleImage())
                .into(holder.iv_header);

        if (position == currentPosition) {
            holder.frameLayout_top.setVisibility(View.INVISIBLE);
            holder.surfaceView.setVisibility(View.VISIBLE);
            player.reset();
            try {
                player.setDisplay(holder.surfaceView.getHolder());
                //TODO
                player.setDataSource(res.aboutRadioUrl);
                player.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{

            holder.frameLayout_top.setVisibility(View.VISIBLE);
            holder.surfaceView.setVisibility(View.INVISIBLE);
        }

        holder.frameLayout_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer tag = (Integer) v.getTag();
                currentPosition = tag;
                if (player != null && player.isPlaying()) {
                    player.stop();
                }
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        SurfaceHolder surfaceHolder;
        SurfaceView surfaceView;
        FrameLayout frameLayout_top;
        ImageView iv_abbreviations, iv_header;
        TextView tv_title, tv_player_number, tv_name, tv_video_time;
        LinearLayout linearLayout;
        public MyHolder(View itemView) {
            super(itemView);
            surfaceView = (SurfaceView) itemView.findViewById(R.id.surface_view);
            frameLayout_top = (FrameLayout) itemView.findViewById(R.id.top);
            iv_abbreviations = (ImageView) itemView.findViewById(R.id.iv_abbreviations);
            iv_header = (ImageView) itemView.findViewById(R.id.iv_header);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_player_number = (TextView) itemView.findViewById(R.id.tv_player_number);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_video_time = (TextView) itemView.findViewById(R.id.tv_video_time);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.ll_video);
            surfaceHolder = surfaceView.getHolder();
        }
    }

}
